//
//  SceneryViewController.m
//  Lovelife
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//


#import "SceneryViewController.h"
#import "SceneryView.h"
#import "SceneryModel.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "SceneryDetailsController.h"
#import "PhotosViewController.h"
#import "NightManager.h"
@interface SceneryViewController ()<pageCutDelegate, photoShowDelegate>
@property (nonatomic, retain) UIActivityIndicatorView *activityIndicatorView;
@property(nonatomic, retain)SceneryModel *model;
@property(nonatomic, retain)SceneryView *inter;
@property(nonatomic, copy)NSString *cityID;
@property (nonatomic, retain) NSMutableArray *collectionArr;
@property (nonatomic, copy) NSString *filePath;
@end

#define Width self.view.frame.size.width
#define mysizey frame.size.height
#define myhight frame.origin.y
#define ALERT(msg) [[[UIAlertView alloc] initWithTitle:nil message:msg delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil] show]

@implementation SceneryViewController
- (void)dealloc
{
    [_model release];
    [_inter release];
    [_cityID release];
    [_collectionArr release];
    [_filePath release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"SceneryCollectionFile.plist"];
        self.collectionArr = [NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithFile:self.filePath]];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // 旋转进度轮
    self.activityIndicatorView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 50, 150, 100, 100)] ;
    [self.activityIndicatorView setActivityIndicatorViewStyle: UIActivityIndicatorViewStyleWhiteLarge];
    self.activityIndicatorView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.activityIndicatorView];
    [_activityIndicatorView release];
    [self netWork];
    
    self.inter = [[SceneryView alloc]initWithFrame:self.view.frame];
    
}
- (void)createView
{
    [self.view addSubview:_inter];
    [_inter release];
    self.inter.delegate = self;
    self.inter.delegateSen = self;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(collectionAction)];
}
- (void)collectionAction
{
    // 遍历判断对象是否存在
    BOOL ifContent = NO;
    NSArray *newCollection = [NSKeyedUnarchiver unarchiveObjectWithFile:self.filePath];
    for (SceneryModel *scenery in newCollection) {
        if ([scenery.sceneryId isEqualToString:self.sceneryId]) {
            ifContent = YES;
        }
    }
    if (ifContent == NO) {
        // 通过归档实现对景点的收藏
        [self.collectionArr addObject:self.model];
        [NSKeyedArchiver archiveRootObject:self.collectionArr toFile:self.filePath];
        ALERT(@"收藏成功!");
    }else{
        ALERT(@"改内容已收藏!");
    }
    
}
- (void)netWork
{
    [self.activityIndicatorView startAnimating];
    NSString *strUrl = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/poi_detail?app_installtime=1429487962&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88307393003751&lon=121.5399246767973&page=1&poi_id=%@", self.sceneryId];
    
    AFHTTPRequestOperationManager *maneger = [AFHTTPRequestOperationManager manager];
    [maneger GET:strUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.model = [[SceneryModel alloc]init];
        self.model.times = [NSString stringWithFormat:@"%@", [responseObject objectForKey:@"times"]];
        NSDictionary *dic = [responseObject objectForKey:@"data"];
        [self.model setValuesForKeysWithDictionary:dic];
        self.cityID = [dic objectForKey:@"id"];
        self.inter.model = self.model;
        [self.inter setvalueScenry];
        [self.activityIndicatorView stopAnimating];
        [self createView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error:%@", error);
    }];
}

- (void)pageCut:(NSString *)str
{
    SceneryDetailsController *sdc = [[SceneryDetailsController alloc]init];
    sdc.word = str;
    [self.navigationController pushViewController:sdc animated:YES];
    [sdc release];
}

- (void)photoShow
{
    PhotosViewController *pvc = [[PhotosViewController alloc]init];
    pvc.cityID = self.cityID;
    pvc.hidesBottomBarWhenPushed = YES;

    [self.navigationController pushViewController:pvc animated:YES];
    [pvc release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setLightDayModel
{
    [NightManager setBackgroundColorWithView:self.inter];
}

- (void)setNightDayModel
{
    [NightManager setBackgroundColorWithView:self.inter];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
